﻿using System;

namespace SharpMap.Data
{
    public static class FeatureDataRowExtensions
    {
        public static double BoundingBoxDistance(this FeatureDataRow srcDataRow, FeatureDataRow destDataRow)
        {
            double srcCenterX = (srcDataRow.Geometry.GetBoundingBox().TopLeft.X +
                srcDataRow.Geometry.GetBoundingBox().TopRight.X +
                srcDataRow.Geometry.GetBoundingBox().BottomLeft.X +
                srcDataRow.Geometry.GetBoundingBox().BottomRight.X)/4;

            double srcCenterY = (srcDataRow.Geometry.GetBoundingBox().TopLeft.Y +
                srcDataRow.Geometry.GetBoundingBox().TopRight.Y +
                srcDataRow.Geometry.GetBoundingBox().BottomLeft.Y +
                srcDataRow.Geometry.GetBoundingBox().BottomRight.Y) / 4;

            double destCenterX = (destDataRow.Geometry.GetBoundingBox().TopLeft.X +
                destDataRow.Geometry.GetBoundingBox().TopRight.X +
                destDataRow.Geometry.GetBoundingBox().BottomLeft.X +
                destDataRow.Geometry.GetBoundingBox().BottomRight.X) / 4;

            double destCenterY = (destDataRow.Geometry.GetBoundingBox().TopLeft.Y +
                destDataRow.Geometry.GetBoundingBox().TopRight.Y +
                destDataRow.Geometry.GetBoundingBox().BottomLeft.Y +
                destDataRow.Geometry.GetBoundingBox().BottomRight.Y)/4;

            return Math.Sqrt(Math.Pow(srcCenterX - destCenterX, 2) + 
                Math.Pow(srcCenterY - destCenterY, 2));
        }
    }
}
